<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar">
        <!--<div class="btn-group tool-button mt5">
            <el-button size="medium" @click="openAdd">
                <i class="ti-plus"></i>
                新建菜单
            </el-button>
        </div>
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="openSort">
                排序
            </el-button>
        </div>-->
        <div class="pull-right offscreen-right" style="margin-right: 20px;line-height: 50px;">
            <el-tooltip class="item" effect="dark" content="新建菜单">
                <el-button type="primary" icon="el-icon-plus"  size="mini" @click="openAdd" circle></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="排序">
                <el-button icon="el-icon-sort"  size="mini" @click="openSort" circle></el-button>
            </el-tooltip>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table :data="tableData" style="width: 100%" size="small" border
                  :highlight-current-row="true" row-key="id" lazy
                  :load="loadChild">
            <el-table-column label="菜单名称" header-align="center" prop="name"
                             width="200" :show-overflow-tooltip="true" align="left">

            </el-table-column>

            <el-table-column label="URL" header-align="center" prop="href"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="资源类型" header-align="center" prop="type"
                             :show-overflow-tooltip="true" align="center">
                <template slot-scope="scope">
                    <i v-if="scope.row.type=='menu'" style="font-weight: bold">菜单</i>
                    <i v-if="scope.row.type=='data'">数据</i>
                </template>
            </el-table-column>

            <el-table-column label="权限标识" header-align="center" prop="permission"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="启用状态" header-align="center" prop="disabled" align="center"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <i v-if="scope.row.disabled"
                       class="fa fa-circle text-danger ml5"></i>
                    <i v-if="!scope.row.disabled"
                       class="fa fa-circle text-success ml5"></i>
                </template>
            </el-table-column>

            <el-table-column label="操作" header-align="center" prop="website"
                             :show-overflow-tooltip="true" align="center" width="120">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item v-if="scope.row.type=='menu'"
                                              :command="{type:'add',id:scope.row.id,name:scope.row.name}">
                                添加子菜单
                            </el-dropdown-item>
                            <el-dropdown-item divided
                                              :command="{type:'enable',id:scope.row.id,name:scope.row.name,row:scope.row}">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item
                                    :command="{type:'disable',id:scope.row.id,name:scope.row.name,row:scope.row}">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item v-if="scope.row.type=='menu'" divided
                                              :command="{type:'editMenu',id:scope.row.id,name:scope.row.name}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item v-if="scope.row.type=='data'" divided
                                              :command="{type:'editData',id:scope.row.id,name:scope.row.name}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'delete',id:scope.row.id,name:scope.row.name}"
                                              v-if="scope.row.path!='0001'">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>

    </el-row>
    <el-dialog :close-on-click-modal="false"
            title="新建菜单"
            :visible.sync="addDialogVisible"
            width="50%">
        <el-form :model="formData" ref="addForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item prop="parentId" label="上级菜单" label-width="80px">
                <el-cascader v-if="!isAddFromSub"
                             style="width: 100%"
                             tabindex="1"
                             :options="menuOptions"
                             :show-all-levels="false"
                             v-trim v-model="parentMenu"
                             :fetch-suggestions="menuQuery"
                             @active-item-change="menuChange"
                             @change="menuChange2"
                             @select="menuSelect"
                             placeholder="不选择则为顶级"
                ></el-cascader>
                <el-input v-if="isAddFromSub" type="text" v-trim v-model="formData.parentName" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item prop="name" label="菜单名称">
                <el-input maxlength="100" placeholder="菜单名称"
                          v-trim v-model="formData.name"
                          auto-complete="off" tabindex="3" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="aliasName" label="菜单别名">
                <el-input maxlength="100" placeholder="菜单名称"
                          v-trim v-model="formData.aliasName"
                          auto-complete="off" tabindex="4" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="permission" label="权限标识">
                <el-input maxlength="100" placeholder="权限标识,如 sys.manager.menu"
                          v-trim v-model="formData.permission"
                          auto-complete="off" tabindex="5" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="href" label="URL">
                <el-input maxlength="100" placeholder="URL"
                          v-trim v-model="formData.href"
                          auto-complete="off" tabindex="6" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="icon" label="图标">
                <el-input maxlength="100" placeholder="图标"
                          v-trim v-model="formData.icon"
                          auto-complete="off" tabindex="7" type="text"></el-input>
                <span :class="formData.icon"></span>
            </el-form-item>
            <el-form-item prop="disabled" label="启用状态">
                <el-switch
                        v-trim v-model="formData.disabled"
                        active-color="#ff4949"
                        inactive-color="#13ce66">
                </el-switch>
            </el-form-item>
            <el-form-item prop="type" label="数据权限">
                <el-radio-group @change="formRadioChange" v-trim v-model="formData.children" size="mini">
                    <el-radio label="false">无</el-radio>
                    <el-radio label="true">有</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item
                    v-if="formData.children=='true'"
                    v-for="(menu, index) in formData.buttons"
                    :label="'权限' + (index+1)"
                    :key="menu.key"
                    :prop="'buttons[' + index + ']'"
                    :rules="{required: true,validator: validateMenu,trigger: ['blur','change']}"
            >
                <el-row :gutter="2">
                    <el-col :span="6">
                        <el-input v-trim v-model="menu.name" placeholder="权限名称"></el-input>
                    </el-col>
                    <el-col :span="13">
                        <el-input v-trim v-model="menu.permission" placeholder="权限标识,如 sys.manager.menu.add"></el-input>
                    </el-col>
                    <el-col :span="4">
                        <el-button @click.prevent="formRemoveMenu(menu)" icon="el-icon-delete" size="small"></el-button>
                    </el-col>
                </el-row>
            </el-form-item>
            <el-row v-if="formData.children=='true'" style="text-align: right;padding-right: 17px;">
                <el-button size="small" @click="formAddMenu" icon="el-icon-circle-plus">添加</el-button>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doAdd">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog :close-on-click-modal="false"
            title="修改菜单"
            :visible.sync="editMenuDialogVisible"
            width="50%">
        <el-form :model="formData" ref="editMenuForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item prop="parentId" label="上级菜单" label-width="80px">
                <el-input type="text" v-trim v-model="formData.parentName" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item prop="name" label="菜单名称">
                <el-input maxlength="100" placeholder="菜单名称"
                          v-trim v-model="formData.name"
                          auto-complete="off" tabindex="3" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="aliasName" label="菜单别名">
                <el-input maxlength="100" placeholder="菜单名称"
                          v-trim v-model="formData.aliasName"
                          auto-complete="off" tabindex="4" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="permission" label="权限标识">
                <el-input maxlength="100" placeholder="权限标识,如 sys.manager.menu"
                          v-trim v-model="formData.permission"
                          auto-complete="off" tabindex="5" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="href" label="URL">
                <el-input maxlength="100" placeholder="URL"
                          v-trim v-model="formData.href"
                          auto-complete="off" tabindex="6" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="icon" label="图标">
                <el-input maxlength="100" placeholder="图标"
                          v-trim v-model="formData.icon"
                          auto-complete="off" tabindex="7" type="text"></el-input>
                <span :class="formData.icon"></span>
            </el-form-item>
            <el-form-item prop="disabled" label="启用状态">
                <el-switch
                        v-trim v-model="formData.disabled"
                        active-color="#ff4949"
                        inactive-color="#13ce66">
                </el-switch>
            </el-form-item>
            <el-form-item prop="type" label="数据权限">
                <el-radio-group @change="formRadioChange" v-trim v-model="formData.children" size="mini">
                    <el-radio label="false">无</el-radio>
                    <el-radio label="true">有</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item
                    v-if="formData.children=='true'"
                    v-for="(menu, index) in formData.buttons"
                    :label="'权限' + (index+1)"
                    :key="menu.key"
                    :prop="'buttons[' + index + ']'"
                    :rules="{required: true,validator: validateMenu,trigger: ['blur','change']}"
            >
                <el-row :gutter="2">
                    <el-col :span="6">
                        <el-input v-trim v-model="menu.name" placeholder="权限名称"></el-input>
                    </el-col>
                    <el-col :span="13">
                        <el-input v-trim v-model="menu.permission" placeholder="权限标识,如 sys.manager.menu.add"></el-input>
                    </el-col>
                    <el-col :span="4">
                        <el-button @click.prevent="formRemoveMenu(menu)" icon="el-icon-delete" size="small"></el-button>
                    </el-col>
                </el-row>
            </el-form-item>
            <el-row v-if="formData.children=='true'" style="text-align: right;padding-right: 17px;">
                <el-button size="small" @click="formAddMenu" icon="el-icon-circle-plus">添加</el-button>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="editMenuDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doEditMenu">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog :close-on-click-modal="false"
            title="修改权限"
            :visible.sync="editDataDialogVisible"
            width="50%">
        <el-form :model="formData2" ref="editDataForm" :rules="formRules2" size="small" label-width="80px">
            <el-form-item prop="name" label="权限名称">
                <el-input maxlength="100" placeholder="权限名称"
                          v-trim v-model="formData2.name"
                          auto-complete="off" tabindex="1" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="permission" label="权限标识">
                <el-input maxlength="100" placeholder="权限标识,如 sys.manager.menu.add"
                          v-trim v-model="formData2.permission"
                          auto-complete="off" tabindex="2" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="disabled" label="启用状态">
                <el-switch
                        v-trim v-model="formData2.disabled"
                        active-color="#ff4949"
                        inactive-color="#13ce66">
                </el-switch>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDataDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doEditData">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog :close-on-click-modal="false"
            title="菜单排序"
            :visible.sync="sortDialogVisible"
            width="50%">
        <el-tree
                ref="sortMenuTree"
                :data="sortMenuData"
                draggable
                :allow-drop="sortAllowDrop"
                node-key="id"
                :props="defaultProps"
        >
            <span class="custom-tree-node" slot-scope="scope">
                <span>{{ scope.node.label }}</span>
            </span>
        </el-tree>
        <span slot="footer" class="dialog-footer">
            <el-button @click="sortDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doSort">确 定</el-button>
        </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                addDialogVisible: false,
                editMenuDialogVisible: false,
                editDataDialogVisible: false,
                sortDialogVisible: false,
                isAddFromSub: false,
                tableData: [],//后台取出的数据
                formData: {
                    id: "",
                    parentId: "",
                    parentName: "",
                    type: "menu",
                    target: "data-pjax",
                    children: "false",
                    buttons: []
                },
                formRules: {
                    name: [
                        {required: true, message: '菜单名称', trigger: ['blur', 'change']}
                    ],
                    permission: [
                        {required: true, message: '权限标识', trigger: ['blur', 'change']}
                    ]
                },
                formData2: {},
                formRules2: {
                    name: [
                        {required: true, message: '权限名称', trigger: ['blur', 'change']}
                    ],
                    permission: [
                        {required: true, message: '权限标识', trigger: ['blur', 'change']}
                    ]
                },
                menuOptions: [],
                parentMenu: [],
                sortMenuData: [],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
            }
        },
        methods: {
            //验证动态添加行数据
            validateMenu: function (rule, value, callback) {
                if (value.name == '') {
                    callback(new Error('权限名称不能为空'));
                } else if (value.permission == '') {
                    callback(new Error('权限标识不能为空'));
                } else {
                    callback();
                }
            },
            menuLoadTree: function (val, cb) {
                var url = base + "/platform/sys/menu/tree";
                $.post(url, {pid: val}, function (data) {
                    if (data.code == 0) {
                        cb(data.data);
                    }
                }, "json");
            },
            menuQuery: function (queryString, cb) {
                cb(this.menuOptions);
            },
            menuSelect: function (val) {
            },
            menuTree: function (table, data, pid) {//树形数据的级联查找很是头疼
                var self = this;
                table.findIndex(function (obj) {
                    if (pid == obj.value) {
                        obj.children = data;
                    } else if (obj.children && obj.children.length > 0) {
                        self.menuTree(obj.children, data, pid);
                    }
                });
            },
            menuChange2: function (val) {
                var self = this;
                if (val[0] == "root") {
                    self.parentMenu = [];
                }
            },
            menuChange: function (val) {
                var self = this;
                var pid = val[val.length - 1];
                self.parentMenu = val;
                self.menuLoadTree(pid, function (tree) {
                    self.menuTree(self.menuOptions, tree, pid);
                });
            },
            formRadioChange: function (val) {
                this.formData.children = val;
                if ("true" == val) {
                    this.formData.buttons = [{
                        name: '',
                        permission: '',
                        key: Date.now()
                    }];
                } else {
                    this.formData.buttons = [];
                }
            },
            formRemoveMenu: function (menu) {
                var index = this.formData.buttons.indexOf(menu);
                if (index !== -1) {
                    this.formData.buttons.splice(index, 1);
                }
            },
            formAddMenu: function () {
                this.formData.buttons.push({
                    name: '',
                    permission: '',
                    key: Date.now()
                });
            },
            //排序树加载
            sortMenuLoad: function () {
                var self = this;
                $.post(base + "/platform/sys/menu/menuAll", {}, function (data) {
                    if (data.code == 0) {
                        self.sortMenuData = data.data;
                    }
                }, "json");
            },
            //排序树控制不可跨级拖拽
            sortAllowDrop: function (moveNode, inNode, type) {
                if (moveNode.data.parentId == inNode.data.parentId) {
                    return type == 'prev';
                }
            },
            //打开排序功能
            openSort: function () {
                this.sortDialogVisible = true;
                this.sortMenuLoad();
            },
            //递归获取所有的ID值
            getTreeIds: function (ids, data) {
                var self = this;
                data.forEach(function (obj) {
                    ids.push(obj.id);
                    if (obj.children && obj.children.length > 0) {
                        self.getTreeIds(ids, obj.children);
                    }
                });
            },
            //提交排序后的数据
            doSort: function () {
                var self = this;
                var ids = [];
                self.getTreeIds(ids, self.sortMenuData);
                $.post(base + "/platform/sys/menu/sortDo", {ids: ids.toString()}, function (data) {
                    if (data.code == 0) {
                        self.$message({
                            message: data.msg,
                            type: 'success'
                        });
                        self.sortDialogVisible = false;
                        self.treeTableChild();
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            openAdd: function () {
                var self = this;
                self.isAddFromSub = false;
                self.addDialogVisible = true;
                self.parentMenu = [];
                //表单还原为初始值
                self.formData = {
                    id: "",
                    parentId: "",
                    parentName: "",
                    type: "menu",
                    target: "data-pjax",
                    children: "false",
                    buttons: []
                };
                if (this.$refs["addForm"])
                    this.$refs["addForm"].resetFields();
                self.menuLoadTree("", function (val) {
                    self.menuOptions = val;
                });
            },
            doAdd: function () {
                var self = this;
                var url = base + "/platform/sys/menu/addDo";
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        if (!self.isAddFromSub) {//如果不是添加子菜单，则从级联选择框取父节点ID
                            self.formData.parentId = self.parentMenu[self.parentMenu.length - 1] || "";
                        }
                        $.post(url, {
                            menu: JSON.stringify(self.formData),
                            buttons: JSON.stringify(self.formData.buttons)
                        }, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                                self.initTreeTable();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            doEditMenu: function () {
                var self = this;
                var url = base + "/platform/sys/menu/editMenuDo";
                self.$refs["editMenuForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, {
                            menu: JSON.stringify(self.formData),
                            buttons: JSON.stringify(self.formData.buttons)
                        }, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });

                                self.editMenuDialogVisible = false;
                                self.initTreeTable();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            doEditData: function () {
                var self = this;
                var url = base + "/platform/sys/menu/editDataDo";
                self.$refs["editDataForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData2, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.editDataDialogVisible = false;
                                self.initTreeTable();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            initTreeTable: function () {
                var self = this;
                var url = base + "/platform/sys/menu/child";
                $.post(url, {pid: ""}, function (data) {
                    if (data.code == 0) {
                        self.tableData = data.data;
                    }
                }, "json");

            },
            loadChild: function (tree, treeNode, resolve) {
                var url = base + "/platform/sys/menu/child";
                $.post(url, {pid: tree.id}, function (data) {
                    if (data.code == 0) {
                        resolve(data.data);
                    }
                }, "json");
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                if ("add" == command.type) {
                    self.openAdd();
                    self.formData.parentId = command.id;
                    self.formData.parentName = command.name;
                    self.isAddFromSub = true;
                }
                if ("editMenu" == command.type) {
                    $.post(base + "/platform/sys/menu/editMenu/" + command.id, {}, function (data) {
                        if (data.code == 0) {
                            self.formData = data.data;//加载后台表单数据
                            self.editMenuDialogVisible = true;//打开编辑窗口
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                if ("editData" == command.type) {
                    $.post(base + "/platform/sys/menu/editData/" + command.id, {}, function (data) {
                        if (data.code == 0) {
                            self.formData2 = data.data;//加载后台表单数据
                            self.editDataDialogVisible = true;//打开编辑窗口
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                if ("enable" == command.type || "disable" == command.type) {
                    $.post(base + "/platform/sys/menu/" + command.type + "/" + command.id, {}, function (data) {
                        if (data.code == 0) {
                            self.$message({
                                message: data.msg,
                                type: 'success'
                            });
                            if ("disable" == command.type) {
                                command.row.disabled = true;
                            }
                            if ("enable" == command.type) {
                                command.row.disabled = false;
                            }
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                if ("delete" == command.type) {
                    self.$confirm('此操作将删除 ' + command.name, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" == a) {//确认后再执行
                                $.post(base + "/platform/sys/menu/delete/" + command.id, {}, function (data) {
                                    if (data.code == 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.initTreeTable();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            }

        },
        created: function () {
            this.initTreeTable();
        }
    });
</script>
<!--#
}
#-->
